import { _decorator, Component, director, Label, Node, tween, Vec3 } from 'cc';
import { AudioManger } from './AudioManger';
import { Clips, DJ, DJ_TYPE } from './Enums';
import { Global } from './Global';
import { BasePage } from './BasePage';
import { AdManger } from './ad/AdManger';
import { DjManger } from './DjManger';
import { LvLStorage } from './LvLStorage';
const { ccclass, property } = _decorator;

@ccclass('GameOverAction')
export class GameOverAction extends BasePage {
    @property(Node) fail_img: Node = null!;
    @property(Node) history_lvl_img: Node = null!;
    @property(Node) btn_continue: Node = null!;
    @property(Node) btn_replay: Node = null!;
    @property(Node) btn_go_lvl_1: Node = null!;
    @property(Label) histroy_level_label: Label = null!;

    start() {
        super.start();
    }

    open() {
        // 防止重复打开
        if (this.node.position.equals(Vec3.ZERO)) return;

        super.open();

        // fail_img 缩放动画
        this.fail_img.scale = new Vec3(2, 2, 0);
        tween(this.fail_img)
            .to(0.4, { scale: new Vec3(1, 1, 0) }, Global.our_easing)
            .start();

        // history_lvl_img 缩放动画
        this.history_lvl_img.scale = new Vec3(0.1, 0.1, 0);
        tween(this.history_lvl_img)
            .to(0.5, { scale: new Vec3(1, 1, 0) }, Global.our_easing)
            .start();

        if (this.histroy_level_label) {
            this.histroy_level_label.string = LvLStorage.get_history_lvl().toString();
        }

        AudioManger.instance.playSound(Clips.fail);

        if (this.btn_replay) {
            this.btn_replay.active = false;
            this.scheduleOnce(() => { this.btn_replay.active = true; }, 1.4);
        }

        if (this.btn_go_lvl_1) {
            this.btn_go_lvl_1.active = false;
            this.scheduleOnce(() => { this.btn_go_lvl_1.active = true; }, 1.1);
        }
    }

    btn_go_home() {
        this.close();
        director.loadScene("home");
        AdManger.show_interstial();
    }

    btn_restart_game() {
        this.close();
        Global.main_action.restartGame();
        AdManger.show_interstial();
    }

    clear_pins_videos() {
        const pin_arr = Global.layer_empty_action.get_pin_arr();
        if (pin_arr.length === 0) {
            AudioManger.instance.playSound(Clips.btn_1);
            Global.tips_action.show("没有可清理的钉子");
            return;
        }
        if (!DjManger.use_dj(DJ.Btn_5, DJ_TYPE.Type_video)) {
            AudioManger.instance.playSound(Clips.btn_1);
            Global.tips_action.show("当前关卡次数已经用完");
            return;
        }

        AdManger.show_video((result) => {
            if (result === 1) {
                console.log("clear_pins_videos 获取奖励成功");
                this.close();
                Global.bucket_action.put_pins(pin_arr);
            } else {
                Global.tips_action.show("获取奖励失败");
                DjManger.return_used_dj(DJ.Btn_5, DJ_TYPE.Type_video);
            }
        });
    }

    go_lvl_1() {
        AdManger.show_video((result) => {
            if (result === 1) {
                console.log("go_lvl_1 获取奖励成功");
                this.close();
                AdManger.show_interstial();

                LvLStorage.record_cur_lvl(1);
                Global.main_action.restartGame();
            } else {
                Global.tips_action.show("返回失败,需要看完视频");
            }
        });
    }
}
